﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;

namespace School.Teaching
{
    public static class OfflineExam
    {
        private static SchoolEntities db = new SchoolEntities();

        public static IEnumerable<TE_Subject> getSubjectsByGrade(long grade, int Exam_Id)
        {
            return from x in db.TE_Subject where x.Grad_Id == grade && !(db.TE_Offline_Exam_Subject.Any(a => a.Exam_Id == Exam_Id && a.Subject_Id==x.Id)) select x;//
        }
        public static object getSubjectsByGradein(long grade, int Exam_Id)
        {
             db = new SchoolEntities();
            return from x in db.V_Exam_Subject where x.Grad_Id == grade && x.Exam_Id == Exam_Id select x;
        }
        //Offlin_Exam
        public static IEnumerable<TE_Offline_Exam> Get_OfflineExamByStudentID(long studentId)
        {
            db = new SchoolEntities();
            var q = from t in db.TE_Student
                    join o in db.ORG_Grade on t.Grade_ID equals o.Id
                    join e in db.TE_Offline_Exam on o.Id equals e.Grad_Id
                    where t.Id==studentId
                    select e;
            return q;
        }
        public static bool Add_OfflineExam(string Name, int Grad_Id, int Ed_Y)
        {
           
            try
            {

                TE_Offline_Exam Te = new TE_Offline_Exam();
                Te.Name = Name;
                Te.Ed_Y = Ed_Y;
                Te.Grad_Id = Grad_Id;
                db.AddToTE_Offline_Exam(Te);
                db.SaveChanges();
                return true;

            }
            catch (Exception)
            {
                return false;

            }
        }
        public static bool Delete_OfflinExam(int Id)
        {
            var deletequery = (from b in db.TE_Offline_Exam
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Updat_OfflinExam(int id, string Name)
        {
            var Update = (from b in db.TE_Offline_Exam
                          where b.Id == id
                          select b).Single();

            Update.Name = Name;
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //offline_Exam_subject
        public static bool Add_OfflineExam_Sub(int Exam_Id, int Subject_Id, decimal Max, decimal Min, decimal duration, DateTime date)
        {
            try
            {
                TE_Offline_Exam_Subject Te = new TE_Offline_Exam_Subject();
                Te.Exam_Id = Exam_Id;
                Te.Subject_Id = Subject_Id;
                Te.Max = Max;
                Te.Min = Min;
                Te.Duration = duration;
                Te.Date = date;
                db.AddToTE_Offline_Exam_Subject(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }

        //Exam-class
        public static IEnumerable<ORG_Class> GetClassBygrad(int grad_id)
        {
            var query = from c in db.ORG_Class select c;
            return query.ToList().Where(c => c.Grade_Id == grad_id);
        }
        //

        public static long Add_Subject_Success_polic(int Sub_Id, decimal Max_degree, decimal Min_degree)
        {
            long id = 0;
            try
            {

                TE_Subject_Success_policy Te = new TE_Subject_Success_policy();
                Te.Sub_Id = Sub_Id;
                Te.Max_degree = Max_degree;
                Te.Min_degree = Min_degree;
                db.AddToTE_Subject_Success_policy(Te);
                db.SaveChanges();
                id = Te.Id;
                return id;

            }
            catch (Exception)
            {
                return id = 0;

            }
        }
        public static bool Delete_Subject_Success_polic(int Id)
        {
            var deletequery = (from b in db.TE_Subject_Success_policy
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Updat_Subject_Success_polic(int id, decimal Max_degree, decimal Min_degree)
        {
            var Update = (from b in db.TE_Subject_Success_policy
                          where b.Id == id
                          select b).Single();

            Update.Max_degree = Max_degree;
            Update.Min_degree = Min_degree;
           
            try
            {
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //offline_Exam_subject
        public static bool Add_Subject_Policy_Exam(int Exam_Id, int Subject_policy_Id, decimal Parcent)
        {
            try
            {
                TE_Subject_Policy_Exam Te = new TE_Subject_Policy_Exam();
                Te.Exam_Id = Exam_Id;
                Te.Subject_policy_Id = Subject_policy_Id;
                Te.Parcent = Parcent;
                db.AddToTE_Subject_Policy_Exam(Te);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public static bool Delete_Subject_Policy_Exam(int exam_Id, int policy_id)
        {
            var deletequery = (from b in db.TE_Subject_Policy_Exam
                               where b.Exam_Id == exam_Id && b.Subject_policy_Id == policy_id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static object Get_Subject_By_Grad(int Grad_id)
        {

            return (from a in db.TE_Subject
                    where a.Grad_Id == Grad_id
                    select a
                 );

        }
        public static long Education_Yearid()
        {

            var id = from y in db.ORG_EductionalYear where y.IsCurrent == true select y;
            return id.FirstOrDefault().Id;
        }
        //////////

        /*public static bool Add_Grad_Success_Policy(int Sub_Id, int Grad_Id, byte Typee)
        {

            try
            {

                TE_Grad_Success_Policy Te = new TE_Grad_Success_Policy();
                Te.Subject_Id = Sub_Id;
                Te.Grad_Id = Grad_Id;
                Te.Type = Typee;
                db.AddToTE_Grad_Success_Policy(Te);
                db.SaveChanges();

                return true;

            }
            catch (Exception)
            {
                return false;

            }
        }
        public static bool Delete_Grad_Success_Policy(int Grad_Id)
        {
            var deletequery = (from b in db.TE_Grad_Success_Policy
                               where b.Grad_Id == Grad_Id
                               select b).ToList();
            try
            {
                if (deletequery != null)
                {
                    foreach (var item in deletequery)
                    {
                        db.DeleteObject(item);
                    }

                    db.SaveChanges();
                    return true;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }*/
        public static bool Delete_Offline_Exam_Subject(int Id)
        {
            var deletequery = (from b in db.TE_Offline_Exam_Subject
                               where b.Id == Id
                               select b).Single();
            try
            {
                db.DeleteObject(deletequery);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Updat_Offline_Exam_Subject(int id, decimal Max_degree, decimal Min_degree, decimal duration, DateTime date)
        {
            var Update = (from b in db.TE_Offline_Exam_Subject
                          where b.Id == id
                          select b).Single();

            Update.Max = Max_degree;
            Update.Min = Min_degree;
            Update.Date = date;
            Update.Duration = duration;
            try
            {
                
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static object Get_Exam(int Grad_id)
        {

            return (from a in db.TE_Offline_Exam
                    where a.Grad_Id == Grad_id
                    select a
                 );

        }


        public static IEnumerable<TE_Offline_Exam_Subject> GetSubjectsByExam(long exam)
        {
            return (from x in db.TE_Offline_Exam_Subject where x.Exam_Id == exam select x).ToList();
        }

        public static IEnumerable<TE_Student> GetStudentsByExam(long exam)
        {
            var grade = (from x in db.TE_Offline_Exam where x.Id == exam select x.Grad_Id).First();

            return (from x in db.TE_Student where x.Grade_ID == grade select x).ToList();
        }

        public static IEnumerable<TE_Offline_Exam_Degree> GetExamDegrees(long exam)
        {
            return (from x in db.TE_Offline_Exam_Degree where x.Exam_Id == exam select x).ToList();
            
            
        }
    }
}
